﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OverStore.Configuration;
using System.Data;
using OverStore.Sql;
using OverStore.Sql.TableSchema;

namespace OverStore.TutorialAsTest.UnitTest.Components.SqlBuilder
{
    [TestClass]
    public class SqlBuilderTest
    {
        private readonly IEnumerable<Table> IndependentTables =
            new[] 
            {
                new Table 
                { 
                    Name = "Cats", 
                    TypeId = "c", 
                    Columns = new ColumnBase [] 
                    {
                        new Key<int>        { Name = "CatId" },
                        new Column<string>  { Name = "Name" },
                        new Column<string>  { Name = "Color" },
                        new Column<string>  { Name = "Sex" }
                    }
                },
                new Table 
                {
                    Name = "Dogs", TypeId = "d",
                    Columns = new ColumnBase []
                    {
                        new Key<int>        { Name = "DogId" },
                        new Column<string>  { Name = "Nick"},
                        new Column<string>  { Name = "Sex" },
                        new Column<string>  { Name = "IsClearBreed" }
                    }
                },
                new Table 
                {
                     Name = "Hourse", TypeId = "h",
                     Columns = new ColumnBase [] 
                     {
                         new Key<int>       { Name = "HourseId"},
                         new Column<int>    { Name = "Owner" },
                         new Column<string> { Name = "TotalizatorName" },
                         new Column<string> { Name = "Sex" }
                     }
                }
            };

        private readonly string CreateTablePerClassInheritanseSelectSingle_ActualSql = "SELECT 'c' AS [TypeId], [CatId], [Name], [Color], [Sex], NULL AS [DogId], NULL AS [Nick], NULL AS [IsClearBreed], NULL AS [HourseId], NULL AS [Owner], NULL AS [TotalizatorName] FROM [Cats] WHERE [CatId] = @Id\r\n	UNION\r\nSELECT 'd' AS [TypeId], NULL AS [CatId], NULL AS [Name], NULL AS [Color], [Sex], [DogId], [Nick], [IsClearBreed], NULL AS [HourseId], NULL AS [Owner], NULL AS [TotalizatorName] FROM [Dogs] WHERE [DogId] = @Id\r\n	UNION\r\nSELECT 'h' AS [TypeId], NULL AS [CatId], NULL AS [Name], NULL AS [Color], [Sex], NULL AS [DogId], NULL AS [Nick], NULL AS [IsClearBreed], [HourseId], [Owner], [TotalizatorName] FROM [Hourse] WHERE [HourseId] = @Id";

        [TestMethod]
        public void CreateTablePerClassInheritanseSelectSingleQuickIntegral()
        {
            var sql = IndependentTables.CreateTablePerClassInheritanceSelectSingle("TypeId", "Id", new SqlServerCeFormatting());
            Assert.AreEqual(CreateTablePerClassInheritanseSelectSingle_ActualSql, sql);
        }
    }
}
